<?php

declare(strict_types=1);

namespace App\Chat\Controller;

use App\Chat\Annotation\ChatAuth;
use App\Chat\Request\ChatRoomRequest;
use App\Chat\Service\TalkRoomService;
use App\CustHub\Service\ChContactService;
use App\CustHub\Service\ChRoomService;
use App\CustHub\Service\ChRoomTagService;
use Hyperf\Context\Context;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;

#[Controller(prefix: 'ch/chat/room'),ChatAuth]
class RoomController extends ChatBaseController
{
    #[Inject]
    protected TalkRoomService $service;

    #[Inject]
    protected ChContactService $contactService;

    #[Inject]
    protected ChRoomService $roomService;

    #[Inject]
    protected ChRoomTagService $roomTagService;

    #[PostMapping('list')]
    public function list()
    {
        $data = $this->request->all();
        $data['main_uid'] = Context::get('main_uid');
        $data['uid'] = Context::get('m_uid');
        return $this->success($this->roomService->list($data));
    }

    #[PostMapping('tagList')]
    public function tagList(): \Psr\Http\Message\ResponseInterface
    {
        $data = $this->request->all();
        $data['main_uid'] = Context::get('main_uid');
        $data['uid'] = Context::get('m_uid');
        return $this->success( $this->roomTagService->tagList($data));
    }

    /**
     * 创建群聊
     * @param ChatRoomRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('create')]
    public function create(ChatRoomRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $data = $request->all();
        return $this->success($this->service->create($data));
    }

    /**
     * 获取联系人列表
     * @param ChatRoomRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getContactList')]
    public function getContactList(ChatRoomRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $data = $request->all();
        $data['main_uid'] = Context::get('main_uid');
        $data['uid'] = Context::get('m_uid');
        return $this->success($this->contactService->getContactList($data));
    }

    /**
     * 邀请入群
     * @param ChatRoomRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('invite')]
    public function invite(ChatRoomRequest $request)
    {
        $data = $request->all();
        $data['main_uid'] = Context::get('main_uid');
        $data['uid'] = Context::get('m_uid');
        return $this->service->invite($data) ?$this->success() : $this->error();
    }
}